import os
from fastapi.exceptions import RequestValidationError, HTTPException
from .logs import getLogger
from fastapi.responses import JSONResponse
from fastapi import status
from fastapi import Request

logger = getLogger(os.environ.get("APP_NAME"))

def global_http_exception_handler(request:Request,exc:HTTPException):
    logger.error(f"global_http_exception_handlerexc发生异常, {exc.detail}")
    return JSONResponse({
        "code": exc.status_code,
        "err_msg": exc.detail,
        "status": 'Failed'
    })

def global_request_exception_handler(request:Request,exc:RequestValidationError):
    logger.error(f"global_request_exception_handler发生异常, {exc}")
    return  JSONResponse({
        "code": status.HTTP_400_BAD_REQUEST,
        "err_msg": exc.errors()[0],
        "status": "Failed"
    })


